import api from '../../api';
import { message } from 'antd';
import constractCode from '../../routes/performanceAnalysis/netValueAnalysis2/eventConfig';
export default {
  namespace: 'netValueM',

  state: {
    timeData: [],
    tableData1: { columns: [], dataSource: [] },
    tableData2: { columns: [], dataSource: [] },
    tableData3: { columns: [], dataSource: [] },
    echartsData1: { xAxis: [], series: [] },
    echartsData2: { xAxis: [], series: [] },
    tableData4: { columns: [], dataSource: [] },
    tableData5: { columns: [], dataSource: [] },
    beginData: {},
    endData: {},
    PropTypes: '',
    bigEventData: {
      bdDate: '2020-02-13',
      edDate: '2020-02-23',
      series: [],
    },
  },

  effects: {
    //  获取时间区间的可选时间范围
    *findProductTime({ payload, callback }, { call, put }) {
      const res = yield call(api.profitAndLoss.findProductTime, payload);
      if (res && res.data && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            timeData: res,
          },
        });
        callback(res);
      } else {
        message.error(res?.msg);
      }
    },

    // 获取大事记时间线
    *getTimeLine({ payload, callback }, { call, put }) {
      const res = yield call(api.netValue.getTimeLine, payload);
      if (res && res.code == 0) {
        let currentEvents = res.data.currentEvents;
        if (currentEvents && currentEvents.length) {
          currentEvents.map((item, index) => {
            item.num = `0${index + 1}`;
          });
        }
        const tableData1 = {
          // 当日大事记表格数据
          columns: [
            { title: '序号', key: 'num' },
            { title: '当日大事记', key: 'desc' },
          ],
          dataSource: currentEvents,
        };
        const netAssetValue_N = []; // 资产净值 (处理后的值)
        const annualYield7d_N = []; // 七日年化收益率(处理后的值)
        const assetUnit_N = []; // 资产份额(处理后的值)
        const portfolioDividend_N = []; // 分红配送(处理后的值)

        const netAssetValue = res.data.netAssetValue; // 资产净值
        const annualYield7d = res.data.accNetValue; // 七日年化收益率
        const assetUnit = res.data.assetUnit; // 资产份额
        const portfolioDividend = res.data.portfolioDividend; // 分红配送
        if (netAssetValue && netAssetValue.length) {
          netAssetValue.map((item) => {
            const contrastData = item.eventPosType == 'positive' ? constractCode.positive.netAssetValue_P : constractCode.negative.netAssetValue_N;
            contrastData.map((item1) => {
              if (item.desc == item1.title) {
                netAssetValue_N.push({ value: item1.value, event: item1.newTitle, date: item.date });
              }
            });
          });
        }

        if (annualYield7d && annualYield7d.length) {
          annualYield7d.map((item) => {
            const contrastData = item.eventPosType == 'positive' ? constractCode.positive.annualYield7d_P : constractCode.negative.annualYield7d_N;
            contrastData.map((item1) => {
              if (item.desc == item1.title) {
                annualYield7d_N.push({ value: item1.value, event: item1.newTitle, date: item.date });
              }
            });
          });
        }

        if (assetUnit && assetUnit.length) {
          assetUnit.map((item) => {
            const contrastData = item.eventPosType == 'positive' ? constractCode.positive.assetUnit_P : constractCode.negative.assetUnit_N;
            contrastData.map((item1) => {
              if (item.desc == item1.title) {
                assetUnit_N.push({ value: item1.value, event: item1.newTitle, date: item.date });
              }
            });
          });
        }
        if (portfolioDividend && portfolioDividend.length) {
          portfolioDividend.map((item) => {
            portfolioDividend_N.push({ value: 0, event: item.desc, date: item.date });
          });
        }
        const bigEventData = {
          bdDate: res.data.beginDate,
          edDate: res.data.endDate,
          series: [
            { name: res.data.portType == 'monetary_like_fund' ? '七日年化收益率' : '累计单位净值', data: annualYield7d_N },
            { name: '资产份额', data: assetUnit_N },
            { name: '资产净值', data: netAssetValue_N },
            { name: '分红送配', data: portfolioDividend_N },
          ],
        };
        yield put({
          type: 'save',
          payload: {
            tableData1: tableData1,
            bigEventData: bigEventData,
          },
        });
      } else {
        yield put({
          type: 'save',
          payload: {
            tableData1: { columns: [], dataSource: [] },
            bigEventData: {
              bdDate: [],
              edDate: [],
              series: [],
            },
          },
        });
        message.error(res?.msg);
      }
    },
    // 接口2
    *getReNetfrepstat({ payload, callback }, { call, put }) {
      const res = yield call(api.netValue.getReNetfrepstat, payload);
      if (res && res.success == true) {
        const netAssetScaleList = res.data.netAssetScaleList;
        const columns = [
          { title: '资产净值', key: 'scaleInteval' },
          { title: '存续天数', key: 'days' },
        ];
        let tableData3 = {};
        if (res.data.portType == 'monetary_like_fund') {
          const columns3 = [
            { title: '七日年化收益率', key: 'annualYield7dInteval' },
            { title: '存续天数', key: 'days' },
          ];
          const annualYield7dList = res.data.annualYield7dList;
          tableData3 = { columns: columns3, dataSource: annualYield7dList };
        } else {
          const columns3 = [
            { title: '累计单位净值区间', key: 'accuNetValInteval' },
            { title: '存续天数', key: 'days' },
          ];
          const accuNetValList = res.data.accuNetValList;
          tableData3 = { columns: columns3, dataSource: accuNetValList };
        }
        yield put({
          type: 'save',
          payload: {
            tableData2: { columns: columns, dataSource: netAssetScaleList },
            tableData3: tableData3,
          },
        });
      } else {
        message.error('无数据');
      }
    },
    // 接口3
    *getReNetvaluelist({ payload, callback }, { call, put }) {
      const res = yield call(api.netValue.getReNetvaluelist, payload);
      if (res && res.success == true) {
        let data = {};
        const list = res.data.list;
        const columns =
          res.data.portType == 'monetary_like_fund'
            ? [
                { title: '日期', key: 'date' },
                { title: '万份收益', key: 'tenThousandYield' },
                { title: '七日年化收益率(%)', key: 'annualYield7d' },
                { title: '资产净值（元）', key: 'netAssetValue' },
                { title: '资产份额', key: 'assetUnit' },
              ]
            : [
                { title: '日期', key: 'date' },
                { title: '单位净值', key: 'netValue' },
                { title: '累计单位净值', key: 'accuNetValue' },
                { title: '日收益率(%)', key: 'dayChangeRate' },
                { title: '资产净值（元）', key: 'netAssetValue' },
                { title: '资产份额', key: 'assetUnit' },
              ];
        const date = []; // 日期数组
        const sevenDays = []; // 七日年化
        const tenThousand = []; // 万分收益
        const netValue = []; // 单位净值
        const totalNetValue = []; // 累计单位净值
        const assetShare = []; // 资产份额
        const asseNetValue = []; // 资产净值
        const totalList = []; // 右下角全量表格数据
        let bgData = {};
        let edData = {};
        if (list && list.length) {
          list.map((item) => [
            date.push(item.date),
            sevenDays.push(item.annualYield7d),
            tenThousand.push(item.tenThousandYield),
            netValue.push(item.netValue),
            totalNetValue.push(item.accuNetValue),
            assetShare.push(item.assetUnit),
            asseNetValue.push(item.netAssetValue),
          ]);
          bgData = list[0];
          edData = list[list.length - 1];
        }
        const echartsData1 =
          res.data.portType == 'monetary_like_fund'
            ? {
                xAxis: date,
                series: [
                  { data: tenThousand, name: '万份收益' },
                  { data: sevenDays, name: '七日年化收益率', yAxisIndex: 1 },
                ],
              }
            : {
                xAxis: date,
                series: [
                  { data: netValue, name: '单位净值' },
                  { data: totalNetValue, name: '累计单位净值', yAxisIndex: 1 },
                ],
              };
        const echartsData2 = {
          xAxis: date,
          series: [
            { data: assetShare, name: '资产份额' },
            { data: asseNetValue, name: '资产净值', yAxisIndex: 1 },
          ],
        };
        yield put({
          type: 'save',
          payload: {
            tableData5: { columns: columns, dataSource: list },
            echartsData1: echartsData1,
            echartsData2: echartsData2,
            beginData: bgData,
            endData: edData,
            PropTypes: res.data.portType,
          },
        });
      } else {
        message.error('无数据');
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
